﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace BookStore
{
    internal class Log
    {
        //此方法调用于注册与重设密码功能中
        //用于检查用户名、密码以及身份证是否合规
        public static bool CheckUserNameAndPassWordAndID(string us, string pw,string id)
        {
            //用户名至少包含3位长度字母
            string UserNamePattern = @"^(?=.*[a-z]).{3,}$";
            //密码至少包含一位小写字母跟一位数字 长度至少8位
            string PassWordPattern = @"^(?=.*[a-z])(?=.*\d).{8,}$";
            //身份证17位数字最多一位大写字母 长度至少18
            string IDPattern = "^[1-9]\\d{5}(18|19|20|21|22)?\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[Xx])$";
            if (string.IsNullOrEmpty(us) && string.IsNullOrEmpty(pw) && string.IsNullOrEmpty(id) )
            {
                MessageBox.Show("Need a username a Password and a id.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if(string.IsNullOrEmpty(us) && string.IsNullOrEmpty(pw))
            {
                MessageBox.Show("Need a username and a Password", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if(string.IsNullOrEmpty(us) && string.IsNullOrEmpty(id))
            {
                MessageBox.Show("Need a username and a id", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (string.IsNullOrEmpty(pw) && string.IsNullOrEmpty(id))
            {
                MessageBox.Show("Need a password and a id", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (string.IsNullOrEmpty(us))
            {
                MessageBox.Show("Need a username", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (string.IsNullOrEmpty(pw))
            {
                MessageBox.Show("Need a password", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if(string.IsNullOrEmpty(id))
            {
                MessageBox.Show("Need a id", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (!Regex.IsMatch(us,UserNamePattern))
            {
                MessageBox.Show("username should be at least 3 lenght character", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (!Regex.IsMatch(pw,PassWordPattern))
            {
                MessageBox.Show("Password should be at least 8 length and include number and character", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (!Regex.IsMatch(id,IDPattern))
            {
                MessageBox.Show("ID should be at least 18 length and include one character", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else
                return true;
        }

        //此方法调用登录功能中
        //用于检查用户输入是否合规
        public static bool CheckUserNameAndPassWord(string us, string pw)
        {
            //用户名至少包含3位长度字母
            string UserNamePattern = @"^(?=.*[a-z]).{3,}$";
            //密码至少包含一位小写字母跟一位数字 长度至少8位
            string PassWordPattern = @"^(?=.*[a-z])(?=.*\d).{8,}$";
            
            if (string.IsNullOrEmpty(us) && string.IsNullOrEmpty(pw))
            {
                MessageBox.Show("Need a username and a Password.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (string.IsNullOrEmpty(us))
            {
                MessageBox.Show("Need a username", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (string.IsNullOrEmpty(pw))
            {
                MessageBox.Show("Need a password", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (!Regex.IsMatch(us, UserNamePattern))
            {
                MessageBox.Show("username should be at least 3 lenght character", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (!Regex.IsMatch(pw, PassWordPattern))
            {
                MessageBox.Show("Password should be at least 8 length and include number and character", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else
                return true;
        }

        //此方法调用于插入书本记录 更新书本记录 删除书本记录
        //用于检查书籍姓名文本框是否有内容
        public static bool CheckBookname(string bookname)
        {
            if(string.IsNullOrEmpty(bookname)) 
            {
                MessageBox.Show("you must inter the bookname!","Alert",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return false;
            }
            else 
            {
                return true;
            }
        }
    }
}
